package com.qdhh.enrollment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qdhh.enrollment.domain.Guide;
import com.qdhh.enrollment.dto.GuideDTO;
import com.qdhh.enrollment.mapper.GuideMapper;
import com.qdhh.enrollment.service.GuideService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class GuideServiceImpl implements GuideService {

    private final GuideMapper guideMapper;

/**
 * 初始化方法，在依赖注入完成后自动执行
 * 用于初始化引导信息数据
 */
@PostConstruct
    public void initSeed() {
        try {
            seedIfEmpty();
        } catch (Exception e) {
            log.warn("Guide seed skipped: {}", e.getMessage());
        }
    }

    @Override
    public List<GuideDTO> list(String type) {
        LambdaQueryWrapper<Guide> wrapper = new LambdaQueryWrapper<Guide>()
            .eq(Guide::getEnabled, 1)
            .orderByAsc(Guide::getOrderNum)
            .orderByAsc(Guide::getCreatedAt);

        if (StringUtils.hasText(type)) {
            wrapper.eq(Guide::getType, type);
        }

        List<Guide> guides = guideMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(guides)) {
            return List.of();
        }
        return guides.stream()
            .map(this::toDto)
            .collect(Collectors.toList());
    }

    @Override
    public void seedIfEmpty() {
        Long count = guideMapper.selectCount(null);
        if (count != null && count > 0) {
            return;
        }

        LocalDateTime now = LocalDateTime.now();
        List<Guide> seeds = Arrays.asList(
            buildGuide("seed-arrival-checklist", "到校准备清单", "arrival",
                "确认学费缴纳、抵达行程、健康状况等信息，按照报到时间安排入校。", "https://example.com/arrival-checklist", 5, now),
            buildGuide("seed-dorm-supplies", "宿舍生活用品指南", "dorm",
                "床上用品、洗漱用品、电器等建议提前准备，办理入住时可现场购买补充。", "https://example.com/dorm-prepare", 10, now),
            buildGuide("seed-international-office", "国际学生报到", "international",
                "抵达后尽快前往国际交流中心办理居留许可、保险及信息备案。", "https://example.com/international-office", 15, now),
            buildGuide("seed-aid-process", "资助办理流程", "aid",
                "了解助学贷款、奖助学金申请时间节点与材料清单，按指引完成申请。", "https://example.com/aid-guide", 20, now)
        );

        seeds.forEach(guideMapper::insert);
    }

    private Guide buildGuide(String id, String title, String type, String content,
                             String link, int orderNum, LocalDateTime createdAt) {
        Guide guide = new Guide();
        guide.setId(id);
        guide.setTitle(title);
        guide.setType(type);
        guide.setContent(content);
        guide.setLink(link);
        guide.setOrderNum(orderNum);
        guide.setEnabled(1);
        guide.setCreatedAt(createdAt);
        return guide;
    }

    private GuideDTO toDto(Guide guide) {
        GuideDTO dto = new GuideDTO();
        dto.setId(guide.getId());
        dto.setTitle(guide.getTitle());
        dto.setType(guide.getType());
        dto.setContent(guide.getContent());
        dto.setLink(guide.getLink());
        dto.setOrderNum(guide.getOrderNum());
        return dto;
    }
}
